package com.imooc.servlet;

import com.imooc.config.PageConfig;
import com.imooc.service.QueryService;
import com.imooc.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 列表页面初始化控制
 */
@WebServlet("/List")
public class ListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String command = request.getParameter("command");
        String description = request.getParameter("description");
        String currentPage = request.getParameter("currentPage");
        request.setAttribute("command", command);
        request.setAttribute("description", description);
        QueryService queryService = new QueryService();

        // 查询消息列表并传给页面
        PageConfig pageConfig = new PageConfig();
        if(StringUtil.isBlank(currentPage) || Integer.parseInt(currentPage) <= 0) {
            pageConfig.setCurrentPage(1);
        } else {
            pageConfig.setCurrentPage(Integer.parseInt(currentPage));
        }
        request.setAttribute("messageList", queryService.queryListByPage(command, description, pageConfig));
        request.setAttribute("pageConfig", pageConfig);
        request.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
